<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BlogController;
use App\Http\Controllers\CommentController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\FollowController;
use App\Http\Controllers\UserPostController;
use App\Http\Controllers\Api\PostLikeController;
use App\Http\Controllers\Api\PostFavoriteController;
use App\Http\Controllers\Api\NotificationController;

// 博客首页
Route::get('/', [BlogController::class, 'index'])->name('blog.index');

// 文章详情
Route::get('/posts/{slug}', [BlogController::class, 'show'])->name('blog.show');

// 分类页面
Route::get('/categories/{slug}', [BlogController::class, 'category'])->name('blog.category');

// 标签页面
Route::get('/tags/{slug}', [BlogController::class, 'tag'])->name('blog.tag');

// 作者页面
Route::get('/authors/{username}', [BlogController::class, 'author'])->name('blog.author');

// 评论功能
Route::middleware(['auth'])->group(function () {
    Route::post('/posts/{post}/comments', [CommentController::class, 'store'])->name('comments.store');
    Route::delete('/comments/{comment}', [CommentController::class, 'destroy'])->name('comments.destroy');
});

// 认证路由
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::post('/login', [AuthController::class, 'login']);
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'register']);
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');

// 用户个人中心路由
Route::prefix('profile')->name('profile.')->group(function () {
    // 需要登录的功能
    Route::middleware(['auth'])->group(function () {
        Route::get('/edit', [ProfileController::class, 'edit'])->name('edit');
        Route::post('/update', [ProfileController::class, 'update'])->name('update');
        Route::post('/avatar', [ProfileController::class, 'uploadAvatar'])->name('avatar');
        Route::get('/history', [ProfileController::class, 'history'])->name('history');
        Route::get('/posts', [ProfileController::class, 'myPosts'])->name('posts');
        Route::get('/drafts', [ProfileController::class, 'drafts'])->name('drafts');
        Route::get('/notifications', [ProfileController::class, 'notifications'])->name('notifications');
        Route::get('/favorites', [ProfileController::class, 'favorites'])->name('favorites');
        Route::get('/follows', [ProfileController::class, 'follows'])->name('follows');
        Route::get('/settings', [ProfileController::class, 'settings'])->name('settings');
        Route::post('/settings', [ProfileController::class, 'updateSettings'])->name('settings.update');
    });

    // 公开的个人主页（放在最后，避免路由冲突）
    Route::get('/{user}', [ProfileController::class, 'show'])->name('show');
});

// 关注功能路由
Route::middleware(['auth'])->prefix('follow')->name('follow.')->group(function () {
    Route::post('/{user}', [FollowController::class, 'follow'])->name('follow');
    Route::delete('/{user}', [FollowController::class, 'unfollow'])->name('unfollow');
    Route::get('/{user}/following', [FollowController::class, 'following'])->name('following');
    Route::get('/{user}/followers', [FollowController::class, 'followers'])->name('followers');
});

// 用户发布文章路由
Route::middleware(['auth'])->prefix('user/posts')->name('user.posts.')->group(function () {
    Route::get('/create', [UserPostController::class, 'create'])->name('create');
    Route::post('/', [UserPostController::class, 'store'])->name('store');
    Route::get('/{post}/edit', [UserPostController::class, 'edit'])->name('edit');
    Route::put('/{post}', [UserPostController::class, 'update'])->name('update');
    Route::delete('/{post}', [UserPostController::class, 'destroy'])->name('destroy');
});

// API路由
Route::prefix('api')->middleware(['auth'])->group(function () {
    // 点赞功能
    Route::prefix('posts/{post}')->group(function () {
        Route::post('/like', [PostLikeController::class, 'toggle'])->name('api.posts.like');
        Route::get('/like/check', [PostLikeController::class, 'check'])->name('api.posts.like.check');
        Route::post('/favorite', [PostFavoriteController::class, 'toggle'])->name('api.posts.favorite');
        Route::get('/favorite/check', [PostFavoriteController::class, 'check'])->name('api.posts.favorite.check');
    });

    // 用户点赞和收藏列表
    Route::get('/user/likes', [PostLikeController::class, 'userLikes'])->name('api.user.likes');
    Route::get('/user/favorites', [PostFavoriteController::class, 'userFavorites'])->name('api.user.favorites');
    Route::post('/posts/favorites/check-batch', [PostFavoriteController::class, 'checkBatch'])->name('api.posts.favorites.check-batch');

    // 通知功能
    Route::prefix('notifications')->name('api.notifications.')->group(function () {
        Route::get('/', [NotificationController::class, 'index'])->name('index');
        Route::get('/unread-count', [NotificationController::class, 'unreadCount'])->name('unread-count');
        Route::post('/{notification}/mark-as-read', [NotificationController::class, 'markAsRead'])->name('mark-as-read');
        Route::post('/mark-all-as-read', [NotificationController::class, 'markAllAsRead'])->name('mark-all-as-read');
        Route::delete('/{notification}', [NotificationController::class, 'destroy'])->name('destroy');
        Route::post('/destroy-batch', [NotificationController::class, 'destroyBatch'])->name('destroy-batch');
        Route::post('/clear', [NotificationController::class, 'clear'])->name('clear');
        Route::get('/settings', [NotificationController::class, 'settings'])->name('settings');
        Route::post('/settings', [NotificationController::class, 'updateSettings'])->name('settings.update');
    });
});
